package com.hpin.mlxteacher.child;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.AttendanceAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.AttendanceResult;
import com.hpin.mlxteacher.utils.AbDateUtil;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

//import com.umeng.analytics.MobclickAgent;

/**
 * 考勤页面
 * Created by Aaron on 2015/7/28.
 */
public class AttendanceActivity extends BaseActivity {

    //红线
    private TextView tv_red_line;
    //屏幕的宽度
    private int screenwidth;
    //当前显示的 是上午还是中午还是下午 0上午,1中午,2下午
    private int currentMessageIndex;

    //是上午还是中午还是下午 0上午,1中午,2下午
    private int signType;
    private GridView gv_child;
    private AttendanceAdapter mAdapter;
    private TextView tv_am;
    private TextView tv_noon;
    private TextView tv_pm;

    private List<AttendanceResult.AttendanceDetail> attendanceDetails;
    //实到多少人
    private TextView tv_act_people;
    //一共多少人
    private TextView total_people;
    //签到人数
    private int signCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_attendance);

        initWidget();

        attendanceDetails = new ArrayList<AttendanceResult.AttendanceDetail>();

        mAdapter = new AttendanceAdapter(this, attendanceDetails, currentMessageIndex);

        gv_child.setAdapter(mAdapter);

        getAttendanceData();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);


        tv_title.setText(getResources().getString(R.string.attendance));
        tv_right.setText(getResources().getString(R.string.history));

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        tv_am = (TextView) findViewById(R.id.tv_choose_left);
        tv_noon = (TextView) findViewById(R.id.tv_choose_middle);
        tv_pm = (TextView) findViewById(R.id.tv_choose_right);

        //日期
        TextView tv_date = (TextView) findViewById(R.id.tv_date);
        Calendar now = Calendar.getInstance();
        String month = ((now.get(Calendar.MONTH) + 1) < 10) ? ("0" + (now.get(Calendar.MONTH) + 1)) : (((now.get(Calendar.MONTH) + 1)) + "");
        String day = now.get(Calendar.DAY_OF_MONTH) < 10 ? "0" + now.get(Calendar.DAY_OF_MONTH) : now.get(Calendar.DAY_OF_MONTH) + "";
//        String today = now.get(Calendar.YEAR) + "年" + month + "月" + day + "日";
        tv_date.setText(now.get(Calendar.YEAR) + "年" + month + "月" + day + "日");

        tv_act_people = (TextView) findViewById(R.id.tv_act_people);
        total_people = (TextView) findViewById(R.id.total_people);

        tv_am.setText(getResources().getString(R.string.am));
        tv_noon.setText(getResources().getString(R.string.noon));
        tv_pm.setText(getResources().getString(R.string.pm));

        gv_child = (GridView) findViewById(R.id.gv_child);

        tv_red_line = (TextView) findViewById(R.id.tv_red_line);

        tv_am.setOnClickListener(this);
        tv_noon.setOnClickListener(this);
        tv_pm.setOnClickListener(this);

        screenwidth = CommonUtils.getScreenSize(this)[0];

        //默认显示上午
        currentMessageIndex = 0;

    }
//
//    public void onResume() {
//        super.onResume();
//        MobclickAgent.onResume(this);       //统计时长
//    }
//
//    public void onPause() {
//        super.onPause();
//        MobclickAgent.onPause(this);
//    }

    /**
     * 签到人数+1
     */
    public void addSignPeople() {

        tv_act_people.setText("实到" + (++signCount) + "人");

    }

    /**
     * 签到人数-1
     */
    public void removeSignPeople() {

        tv_act_people.setText("实到" + (--signCount) + "人");

    }

    /**
     * 获取签到信息
     */
    private void getAttendanceData() {

        if (mAdapter != null)
            mAdapter.setType(signType);

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("day", AbDateUtil.getStringByFormat(new Date(), AbDateUtil.dateFormatYMD));
        j.put("classId", sp.getString(Constant.classId, ""));

        HttpUtil.setPostRequest("正在获取签到信息", this, Constant.WEB_ROOT + Constant.CHECKLIST,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {
                        if (CommonUtils.isNull(str)) {
                            showToast(Constant.NODATA);
                            return;
                        }

                        try {

                            AttendanceResult attendanceResult = JSONObject.parseObject(str, AttendanceResult.class);

                            if (attendanceResult.code == Constant.SUCCESS) {

                                attendanceDetails = attendanceResult.data;
//                                attendanceDetails.clear();
                                //计算去哪办共有多少人以及实到多少人
                                total_people.setText("全班" + attendanceDetails.size() + "人");

                                signCount = 0;//置为0,防止重复添加
                                switch (signType) {

                                    case 0://上午

                                        for (int i = 0; i < attendanceDetails.size(); i++) {

                                            if (attendanceDetails.get(i).signM == 1) {

                                                ++signCount;
                                            }
                                        }

                                        break;

                                    case 1://中午

                                        for (int i = 0; i < attendanceDetails.size(); i++) {

                                            if (attendanceDetails.get(i).signA == 1) {

                                                ++signCount;
                                            }
                                        }

                                        break;

                                    case 2://下午

                                        for (int i = 0; i < attendanceDetails.size(); i++) {

                                            if (attendanceDetails.get(i).signN == 1) {

                                                ++signCount;
                                            }
                                        }

                                        break;

                                }

//                                tv_act_people.setText("实到" + signCount + "人");
                                tv_act_people.setText(Html.fromHtml("实到" + "<font color='#ff0000'>" + (signCount) + "</font>" + "人"));
                                mAdapter.setDatas(attendanceDetails);
                                mAdapter.notifyDataSetChanged();

                            } else {

                                showToast(Constant.showErrorMsg(attendanceResult.code, AttendanceActivity.this));

                            }

                        } catch (
                                Exception e
                                )

                        {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }

                    }
                }, null);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
            case R.id.tv_right://历史

                Intent hIntent = new Intent(this, HistoryActivity.class);
                startActivity(hIntent);

                break;

            case R.id.tv_choose_left://上午
                changeTextColor2Gray();
                tv_am.setTextColor(getResources().getColor(R.color.red));
                changeRedLine(currentMessageIndex, 0);

//                mAdapter.setType(0);
//                mAdapter.notifyDataSetChanged();
                signType = 0;

                getAttendanceData();

                break;

            case R.id.tv_choose_middle://中午
                changeTextColor2Gray();
                tv_noon.setTextColor(getResources().getColor(R.color.red));
                changeRedLine(currentMessageIndex, 1);

//                mAdapter.notifyDataSetChanged();

                signType = 1;

                getAttendanceData();

                break;

            case R.id.tv_choose_right://下午
                changeTextColor2Gray();
                tv_pm.setTextColor(getResources().getColor(R.color.red));
                changeRedLine(currentMessageIndex, 2);

//                mAdapter.setType(2);
//                mAdapter.notifyDataSetChanged();

                signType = 2;

                getAttendanceData();

                break;
        }
    }

    /**
     * 改变红线的位置
     *
     * @param currentIndex 当前红线在哪个位置
     * @param toIndex      到哪个位置
     */
    private void changeRedLine(int currentIndex, int toIndex) {

        ObjectAnimator anim = null;
        //获取当前控件的x距离
        float curTranslationX = tv_red_line.getTranslationX();

//        LogUtil.e("TAG", "curTranslationX==>" + curTranslationX);

        switch (currentIndex) {

            case 0:

                //当前在第一位置
                switch (toIndex) {

                    case 1:

                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth / 3);
                        currentMessageIndex = 1;
                        break;

                    case 2:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth / 3 * 2);
                        currentMessageIndex = 2;
                        break;

                }

                break;

            case 1:

                //当前在第二位置
                switch (toIndex) {

                    case 0:

                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, 0);
                        currentMessageIndex = 0;
                        break;

                    case 2:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth / 3 * 2);
                        currentMessageIndex = 2;
                        break;

                }

                break;

            case 2:

                //当前在第三位置
                switch (toIndex) {

                    case 0:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, 0);
                        currentMessageIndex = 0;
                        break;

                    case 1:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth / 3);
                        currentMessageIndex = 1;
                        break;
                }

                break;

        }

        if (anim != null) {
            //设置移动时间0.5秒s
            anim.setDuration(500);
            anim.start();
        }

    }

    /**
     * 把字体颜色都变灰色
     */
    private void changeTextColor2Gray() {

        tv_am.setTextColor(getResources().getColor(R.color.black));
        tv_noon.setTextColor(getResources().getColor(R.color.black));
        tv_pm.setTextColor(getResources().getColor(R.color.black));

    }

}
